import java.util.LinkedList;
import java.util.Queue;

public class Solution1263 {
    public int minPushBox(char[][] grid) {
        int sx=-1,sy=-1,bx=-1,by=-1,tx=-1,ty=-1;
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[0].length;j++){
                if (grid[i][j]=='S'){
                    sx=i;
                    sy=j;
                }else if (grid[i][j]=='B'){
                    bx=i;
                    by=j;
                }else if (grid[i][j]=='T'){
                    tx=i;
                    ty=j;
                }
            }
        }
        Queue<int[]> queue=new LinkedList<>();
        int[][][][] visit=new int[grid.length][grid[0].length][grid.length][grid[0].length];
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[0].length;j++){
                for (int k=0;k<grid.length;k++){
                    for (int l=0;l<grid[0].length;l++){
                         visit[i][j][k][l]=Integer.MAX_VALUE;
                    }
                }
            }
        }
        visit[sx][sy][bx][by]=0;
        queue.offer(new int[]{sx,sy,bx,by});
        int[] dx=new int[]{0,1,0,-1};
        int[] dy=new int[]{1,0,-1,0};
        while (!queue.isEmpty()){
            int[] t=queue.poll();
            for (int i = 0; i < 4; i++) {
                int x=t[0]+dx[i];
                int y=t[1]+dy[i];
                int add;
                int nsx,nsy,nbx,nby;
                if (x>=0 && x<grid.length && y>=0 && y<grid[0].length && grid[x][y]!='#'){
                    if (t[2]==x && t[3]==y){
                        x=x+dx[i];
                        y=y+dy[i];
                        if (x>=0 && x<grid.length && y>=0 && y<grid[0].length && grid[x][y]!='#'){
                            nsx=t[2];
                            nsy=t[3];
                            nbx=x;
                            nby=y;
                            add=1;
                        }else {
                            continue;
                        }
                    }else {
                        nsx=x;
                        nsy=y;
                        nbx=t[2];
                        nby=t[3];
                        add=0;
                    }
                }else {
                    continue;
                }
                if (visit[nsx][nsy][nbx][nby]>visit[t[0]][t[1]][t[2]][t[3]]+add){
                    visit[nsx][nsy][nbx][nby]=visit[t[0]][t[1]][t[2]][t[3]]+add;
                    queue.offer(new int[]{nsx,nsy,nbx,nby});
                }
            }
        }
        int ans=Integer.MAX_VALUE;
        for (int i=0;i<grid.length;i++){
            for (int j=0;j<grid[0].length;j++){
                ans=Math.min(ans,visit[i][j][tx][ty]);
            }
        }
        return ans==Integer.MAX_VALUE?-1:ans;
    }

    public static void main(String[] args) {
        System.out.println(new Solution1263().minPushBox(new char[][]{{'#','#','#','#','#','#'},{'#','T','#','#','#','#'},{'#','.','.','B','.','#'},{'#','.','#','#','.','#'},{'#','.','.','.','S','#'},{'#','#','#','#','#','#'}}));
    }
}
